package com.code.leetcode.center50;

import com.code.leetcode.utils.TreeNode;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/26 18:58
 */
public class The108 {

    int[] nums;

    public TreeNode sortedArrayToBST(int[] nums) {
        this.nums = nums;
        if (nums.length == 0) return null;
        return helper(0, nums.length - 1);
    }

    private TreeNode helper(int begin, int end) {
        if (begin > end) return null;
        int mid = (begin + end) / 2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = helper(begin, mid - 1);
        node.right = helper(mid + 1, end);
        return node;
    }

}
